package com.hunlian.makelove.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



/**
 * Created by zealjiang on 2016/3/29 14:58.
 */
public class PaintSettingSView extends SurfaceView implements SurfaceHolder.Callback {

    private DrawThread mDrawThread;
    private Context mContext;
    private int mBrushWidth;
    private boolean mNeedRefresh = true;
    private int mWidth,mHeight;
    private Paint mPaint;

    public PaintSettingSView(Context context) {
        super(context, null);
        mContext = context;
    }

    public PaintSettingSView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init(); //初始化,设置生命周期回调方法
    }

    private void init() {
        SurfaceHolder holder = getHolder();
        holder.addCallback(this); //设置Surface生命周期回调
        holder.setFormat(PixelFormat.TRANSPARENT); // 设置为透明
        setZOrderOnTop(true);// 设置为顶端
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        mWidth = this.getWidth();
        mHeight = this.getHeight();

        mPaint = new Paint(); //创建画笔
        mPaint.setColor(Color.BLACK);
        if (null == mDrawThread) {
            mDrawThread = new DrawThread(holder, mContext);
            mDrawThread.start();
            mDrawThread.isRunning = true;
        }

    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        closeDraw();
    }


    /**
     * 执行绘制的绘制线程
     *
     * @author zealjiang
     *         created at 2016/3/29 15:33
     */
    class DrawThread extends Thread {

        SurfaceHolder surfaceHolder;
        Context context;
        boolean isRunning;

        public DrawThread(SurfaceHolder surfaceHolder, Context context) {

            this.surfaceHolder = surfaceHolder;
            this.context = context;
            isRunning = false;

        }

        @Override
        public void run() {

            while(isRunning)
            {
                Canvas c = null;

                if(mNeedRefresh) {
                    try {
                        synchronized (surfaceHolder) {
                            c = surfaceHolder.lockCanvas();//锁定画布，一般在锁定后就可以通过其返回的画布对象Canvas，在其上面画图等操作了。
                            c.drawColor(Color.WHITE);
                            mPaint.setStrokeWidth(mBrushWidth);
                            c.drawLine(100,mHeight/2,mWidth-100,mHeight/2,mPaint);

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (c != null) {
                            surfaceHolder.unlockCanvasAndPost(c);//结束锁定画图，并提交改变。
                        }
                    }
                    mNeedRefresh = false;
                }
            }


        }

    }


    /**
     * 设置画笔的宽度
     * @author zealjiang
     * @time 2016/6/25 17:08
     */
    public void setBrushWidth(int brushWidth) {
        mBrushWidth = brushWidth;
        mNeedRefresh = true;
    }

    /**
     *关闭绘图线程
     *@author zealjiang
     *created at 2016/4/14 16:47
     */
    public void closeDraw(){

        if(mDrawThread==null){
            return;
        }
        mDrawThread.isRunning = false;
        try {
            mDrawThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            mDrawThread = null;
        }
    }
}
